package com.ftg.learn.util;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 从苞米豆 官网直接拿到的策略
 */
@Slf4j
@Component
public class CustomIdGenerator implements IdentifierGenerator {

    private final AtomicLong al = new AtomicLong(1);

    @Override
    public Long nextId(Object entity) {
        //可以将当前传入的class全类名来作为bizKey,或者提取参数来生成bizKey进行分布式Id调用生成.
        String bizKey = entity.getClass().getName();
        System.out.println(bizKey+"===>"+ (bizKey.hashCode() & 31));
        //        MetaObject metaObject = SystemMetaObject.forObject(entity);
//        String name = (String) metaObject.getValue("name"); //admin
//        final long id = al.getAndAdd(1);
//        log.info("为{}生成主键值->:{}", name, id);
        IdWorker worker = new IdWorker(1, bizKey.hashCode() & 31,1);
        return worker.nextId();
    }
}
